<template>
  <div id="comexcelimportresult" v-loading="loading">
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData"
      @downloadExceptionFile="downloadExceptionFile" @downloadImportFile="downloadImportFile"
      :title="$t('transmit.importresult')"></v-tableTitle>
    <v-table :hasEdit="false" :page="page" :pageSize="pageSize" :tableConfig="tableConfig" :tableData="tableData"
      :total="total" @findData="getTableData" @getTableData="getTableData" @searhData="searhData"
      @selectData="selectData" @upData="getTableData" ref="table"></v-table>
    <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";

export default {
  name: "comexcelimportresult",
  mixins: [baseMixin, config],
  data() {
    return {
      loading: false,
      tableData: [],
      page: 1,
      pageSize: 10,
      total: 0,
      tableSelectData: [],
      tableSearhData: {},
    };
  },
  computed: {
    btnConfig() {
      return [
        { label: this.$t('transmit.downloadImportFile'), certigier: false || (!this.btnPermNameList.includes('EN02070301') && !this.btnPermNameList.includes('WM03160301')), invalid: "downloadImportFile" },
        { label: this.$t('transmit.downloadExceptionFile'), certigier: false || (!this.btnPermNameList.includes('EN02070302') && !this.btnPermNameList.includes('WM03160302')), invalid: "downloadExceptionFile" }
      ]
    }
  },
  created() {
  },
  methods: {
    //下载异常的文件
    async downloadExceptionFile(data) {
      console.log(data);
      let self = this;
      if (data.length < 1) {
        this.$message.error(this.$t('msg.msg16'));
        return;
      }
      if (data.length > 1) {
        this.$message.error(this.$t('msg.msg16'));
        return;
      }
      if (data[0].exceptionFileUrl == "" || data[0].exceptionFileUrl == null) {
        self.$message.error(this.$t('msg.msg68'));
        return;
      }
      try {
        let res = await this.$api.post('/comExcelImportResult/downloadExceptionFile', data, { responseType: 'blob' })
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, data[0].exceptionFileName);
        } else {
          var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' });
          var filename = data[0].exceptionFileName;
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    //下载导入的文件
    async downloadImportFile(data) {
      console.log(data);
      let self = this;
      if (data.length < 1) {
        this.$message.error(this.$t('msg.msg16'));
        return;
      }
      if (data.length > 1) {
        this.$message.error(this.$t('msg.msg16'));
        return;
      }
      try {
        let res = await this.$api.post('/comExcelImportResult/downloadImportFile', data, { responseType: 'blob' })
        if (window.navigator.msSaveBlob) {
          var blobObject = new Blob([res.data]);
          window.navigator.msSaveBlob(blobObject, data[0].importFileName);
        } else {
          var blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8' });
          var filename = data[0].importFileName;
          var downloadElement = document.createElement('a');
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.style.display = 'none';
          downloadElement.href = href;
          downloadElement.download = filename; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
        }
      } catch (e) {
        this.$message.error(e);
      } finally {
        
      }
    },
    //选中的内容
    selectData(data) {
      this.tableSelectData = data;
    },
    async getTableData(page, data) {
      try {
        console.log(data);
        this.loading = true;
        let res = await this.$api.post(`/comExcelImportResult/selectComExcelImportResultList/${page}`, data);
        
          this.loading = false;
          this.tableData = res.data.data.list;
          this.page = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
      } catch (e) {
        this.loading = false;
        this.$message.error(e);
      }
    },
    searhData(data, up) {
      if (!up) {
        this.tableSearhData = data;
      } else {
        this.tableSearhData = data;
        this.getTableData(1, this.tableSearhData);
      }
    },

  },
  mounted() {
    this.$store.commit("setSkip", {});
    this.getTableData(1, {});
  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    "v-table": table,
  }
};
</script>

<style scoped>
#comexcelimportresult {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
